#include "Rainbow.h"
#include <avr/pgmspace.h>

unsigned char buffer[2][96] = {
	{
		0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x04, 0xBF,
		0x00, 0x00, 0x4B, 0xFF, 0x00, 0x04, 0xBF, 0xFF,
		0x00, 0x4B, 0xFF, 0xFF, 0x04, 0xBF, 0xFF, 0xFF,
		0x4B, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFF, 0xFD,
		0xFF, 0xFD, 0x71, 0x00, 0xFF, 0xD7, 0x10, 0x00,
		0xFD, 0xF1, 0x00, 0x00, 0xDA, 0x10, 0x00, 0x00,
		0x71, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x17,
		0x00, 0x00, 0x01, 0x7E, 0x00, 0x00, 0x17, 0xEF,
		0x06, 0xEF, 0xFF, 0xFF, 0x6E, 0xFF, 0xFF, 0xFF,
		0xEF, 0xFF, 0xFF, 0xFA, 0xFF, 0xFF, 0xFF, 0xA3,
		0xFF, 0xFF, 0xFA, 0x30, 0xFF, 0xFA, 0xA3, 0x00,
		0xFF, 0xFA, 0x30, 0x00, 0xFF, 0xA3, 0x00, 0x00
	},
	{
		0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x04, 0xBF,
		0x00, 0x00, 0x4B, 0xFF, 0x00, 0x04, 0xBF, 0xFF,
		0x00, 0x4B, 0xFF, 0xFF, 0x04, 0xBF, 0xFF, 0xFF,
		0x4B, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFF, 0xFD,
		0xFF, 0xFD, 0x71, 0x00, 0xFF, 0xD7, 0x10, 0x00,
		0xFD, 0xF1, 0x00, 0x00, 0xDA, 0x10, 0x00, 0x00,
		0x71, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x17,
		0x00, 0x00, 0x01, 0x7E, 0x00, 0x00, 0x17, 0xEF,
		0x06, 0xEF, 0xFF, 0xFF, 0x6E, 0xFF, 0xFF, 0xFF,
		0xEF, 0xFF, 0xFF, 0xFA, 0xFF, 0xFF, 0xFF, 0xA3,
		0xFF, 0xFF, 0xFA, 0x30, 0xFF, 0xFA, 0xA3, 0x00,
		0xFF, 0xFA, 0x30, 0x00, 0xFF, 0xA3, 0x00, 0x00
	}
};

unsigned char whichbuf = 1;
unsigned char bufpos = 0;

void setup() {
	init_rainbow(buffer[0]);
	Serial.begin(9600);
}

void loop() {
	if (Serial.available() > 0) {
		buffer[whichbuf][bufpos++] = Serial.read();
		if (bufpos >= 96) {
			set_next_buffer(buffer[whichbuf]);
			whichbuf ^= 1;
			bufpos = 0;
		}
	}
}

ISR(TIMER2_OVF_vect) {
	timer2_isr();
}
